import { ecs } from "db://oops-framework/libs/ecs/ECS";
import { Account } from "../Account";
import { AccountModelComp } from "../model/AccountModelComp";
import { smc } from "../../common/ecs/SingletonModuleComp";
import { Leader } from "../../leader/Leader";
import { JsonUtil } from "db://oops-framework/core/utils/JsonUtil";
import { TableRoleModel } from "../../common/table/TableRoleModel";
import { Role } from "../../role/Role";
import { oops } from "db://oops-framework/core/Oops";
import { GameEvent } from "../../../type/GameEvent";
import { Skill } from "../../battle/skill/Skill";
import { Equip } from "../../equip/Equip";

/** 初始化游戏帐号 */
@ecs.register('InitAccount')
export class InitAccountComp extends ecs.Comp {
    reset() { }
}
export class InitAccountSystem extends ecs.ComblockSystem implements ecs.IEntityEnterSystem {
    filter(): ecs.IMatcher {
        return ecs.allOf(InitAccountComp, AccountModelComp);
    }

    async entityEnter(e: Account) {
        await this.login(e);
        await this.loadUser(e);
        await this.loadBag(e);
        await this.createRoleBook();
        await this.loadHero(e);
        oops.message.dispatchEvent(GameEvent.LoginSuccess);
        e.remove(InitAccountComp);
    }

    private login(e: Account) {
        return new Promise((resolve, reject) => {
            var params: any = { "uid": e.AccountModel.uid, "subid": e.AccountModel.subid, "token": "qwe123" }

            let onComplete = {
                target: this,
                callback: (data: any) => {
                    resolve(null);
                }
            }
            // 请求登录游戏获取角色数据
            // netChannel.game.req("user", "login", params, onComplete);
            onComplete.callback(null);
        })
    }

    private loadUser(e: Account) {
        return new Promise((resolve, reject) => {
            var params: any = {}
            let onComplete = {
                target: this,
                callback: (data: any) => {
                    var user = data.data.user;
                    //--------玩家id----------
                    smc.playerInfo.PlayerInfoModel.playerId = Math.random() + "" //user.uid;
                    //--------玩家头像----------
                    smc.playerInfo.PlayerInfoModel.avatar = ""// user.avatar;
                    //--------玩家名字----------
                    smc.playerInfo.PlayerInfoModel.name = "玩家" + smc.playerInfo.PlayerInfoModel.playerId// user.nickname;

                    this.createLeader(user);
                    resolve(null);
                }
            }
            // 请求登录游戏获取角色数据
            //  netChannel.game.req("user", "loaduser", params, onComplete);
            onComplete.callback({
                data: {
                    user: {
                        stage: 0,
                        lv: 1
                    }
                }
            });

        })


    }
    private createLeader(data: any) {
        if (smc.account.AccountModel.leader) {
            var leader = smc.account.AccountModel.leader;
        } else {
            var leader = ecs.getEntity<Leader>(Leader);
        }

        leader.LeaderModel.id = 1;
        leader.LeaderModel.uuid = 1;
        leader.LeaderModel.stage = data.stage;
        leader.setLevel(data.lv);

        if (!smc.account.AccountModel.leader) {
            smc.account.AccountModel.leader = leader;
        }
    }

    private loadHero(e: Account) {
        return new Promise((resolve, reject) => {
            var params: any = {}

            let onComplete = {
                target: this,
                callback: (data: any) => {
                    var heros: Array<any> | undefined = data.data.heros;
                    if (heros) {
                        heros.forEach(element => {
                            this.initRole(element);
                        });
                    }
                    // e.checkRoleHalo();
                     e.checkEquipUpgrade();
                    resolve(null);
                }
            }
            // 请求登录游戏获取角色数据
            // netChannel.game.req("user", "loadallhero", params, onComplete);
            let heros: Array<any> =
                [
                    {
                        "hp": 26201,
                        "def": 3445,
                        "spd": 55,
                        "zhanli": 12281,
                        "equipment": {
                            "weapon": {
                                "lv": 130,
                                "setattr": {},
                                "def": 0,
                                "type": 1,
                                "hp": 0,
                                "color": 2,
                                "id": 5005,
                                "atk": 1064
                            },
                            "head": {
                                "lv": 130,
                                "setattr": {},
                                "def": 1185,
                                "type": 3,
                                "hp": 0,
                                "color": 2,
                                "id": 5007,
                                "atk": 0
                            },
                            "horse": {
                                "lv": 129,
                                "setattr": {},
                                "def": 0,
                                "type": 4,
                                "hp": 4262,
                                "color": 2,
                                "id": 5008,
                                "atk": 0
                            },
                            "cloth": {
                                "lv": 129,
                                "setattr": {},
                                "def": 0,
                                "type": 2,
                                "hp": 4616,
                                "color": 2,
                                "id": 5006,
                                "atk": 0
                            }
                        },
                        "star": 0,
                        "yuling": {},
                        "lv": 107,
                        "heroid": 312,
                        "stage": 1,
                        "atk": 4174,
                        "pos": 2,
                        "serverid": 1,
                        "collect_emblemstar": 0,
                        "id": 818995312
                    },
                    {
                        "hp": 7842,
                        "def": 866,
                        "spd": 10,
                        "zhanli": 3059,
                        "equipment": {
                            "weapon": {
                                "lv": 1,
                                "setattr": {},
                                "def": 0,
                                "type": 1,
                                "hp": 0,
                                "color": 2,
                                "id": 5005,
                                "atk": 9
                            },
                            "head": {
                                "lv": 1,
                                "setattr": {},
                                "def": 10,
                                "type": 3,
                                "hp": 0,
                                "color": 2,
                                "id": 5007,
                                "atk": 0
                            },
                            "horse": {
                                "lv": 1,
                                "setattr": {},
                                "def": 0,
                                "type": 4,
                                "hp": 35,
                                "color": 2,
                                "id": 5008,
                                "atk": 0
                            },
                            "cloth": {
                                "lv": 1,
                                "setattr": {},
                                "def": 0,
                                "type": 2,
                                "hp": 37,
                                "color": 2,
                                "id": 5006,
                                "atk": 0
                            }
                        },
                        "star": 0,
                        "yuling": {},
                        "lv": 1,
                        "heroid": 228,
                        "stage": 0,
                        "atk": 761,
                        "pos": 0,
                        "serverid": 1,
                        "collect_emblemstar": 0,
                        "id": 818995228
                    },
                    {
                        "hp": 32202,
                        "def": 3195,
                        "spd": 96,
                        "zhanli": 12140,
                        "equipment": {
                            "weapon": {
                                "lv": 29,
                                "setattr": {},
                                "def": 0,
                                "type": 1,
                                "hp": 0,
                                "color": 2,
                                "id": 5005,
                                "atk": 236
                            },
                            "head": {
                                "lv": 29,
                                "setattr": {},
                                "def": 263,
                                "type": 3,
                                "hp": 0,
                                "color": 2,
                                "id": 5007,
                                "atk": 0
                            },
                            "horse": {
                                "lv": 29,
                                "setattr": {},
                                "def": 0,
                                "type": 4,
                                "hp": 958,
                                "color": 2,
                                "id": 5008,
                                "atk": 0
                            },
                            "cloth": {
                                "lv": 29,
                                "setattr": {},
                                "def": 0,
                                "type": 2,
                                "hp": 1038,
                                "color": 2,
                                "id": 5006,
                                "atk": 0
                            }
                        },
                        "star": 0,
                        "yuling": {},
                        "lv": 200,
                        "heroid": 308,
                        "stage": 1,
                        "atk": 2917,
                        "pos": 1,
                        "serverid": 1,
                        "collect_emblemstar": 0,
                        "id": 818995308
                    },
                    {
                        "hp": 6882,
                        "def": 834,
                        "spd": 11,
                        "zhanli": 2867,
                        "equipment": {
                            "weapon": {
                                "lv": 1,
                                "setattr": {},
                                "def": 0,
                                "type": 1,
                                "hp": 0,
                                "color": 2,
                                "id": 5005,
                                "atk": 9
                            },
                            "head": {
                                "lv": 1,
                                "setattr": {},
                                "def": 10,
                                "type": 3,
                                "hp": 0,
                                "color": 2,
                                "id": 5007,
                                "atk": 0
                            },
                            "horse": {
                                "lv": 1,
                                "setattr": {},
                                "def": 0,
                                "type": 4,
                                "hp": 35,
                                "color": 2,
                                "id": 5008,
                                "atk": 0
                            },
                            "cloth": {
                                "lv": 1,
                                "setattr": {},
                                "def": 0,
                                "type": 2,
                                "hp": 37,
                                "color": 2,
                                "id": 5006,
                                "atk": 0
                            }
                        },
                        "star": 0,
                        "yuling": {},
                        "lv": 1,
                        "heroid": 314,
                        "stage": 0,
                        "atk": 794,
                        "pos": 0,
                        "serverid": 1,
                        "collect_emblemstar": 0,
                        "id": 818995314
                    }]

            setTimeout(() => {
                onComplete.callback({
                    data: {
                        heros: heros
                    }
                });
            }, 100);


        })
    }

    /** 创建角色对象（自定义逻辑） */
    private initRole(data: any) {
        let role = smc.book.BookModel.books.get(data.heroid);
        let equips: any = data.equipment;
        role!.RoleModel.pos = data.pos;
        role!.RoleModel.stage = data.stage;
        role!.RoleModel.hasOpen = 1;
        role!.setStar(data.star);
        role!.setLevel(data.lv);
        role!.RoleModelBook.collectEmblemStar = data.collect_emblemstar;
        role?.checkBookStar();
        if (data.pos) {
            smc.account.AccountModel.battles.set(role!.RoleModel.id, role!);
            smc.account.checkRpShangzhen();;
        }

        for (const key in equips) {
            const equip = equips[key];
            var e1 = role?.RoleModelEquip.equips.get(equip.type);
            e1!.EquipModel.id = equip.id;
            e1!.setLevel(equip.lv);
        }
    }

    /** 创建图鉴对象 */
    private createRoleBook() {
        if (smc.book.BookModel.books.size > 0) {
            return
        }
        var tableRole = JsonUtil.get(TableRoleModel.TableName);
        for (const key in tableRole) {
            if (tableRole[key].type == 2) {
                let role = ecs.getEntity<Role>(Role);

                role.RoleModelBook.id = Number(key);
                role.RoleModel.id = Number(key);
                role.RoleModel.uuid = Number(key);
                role.RoleModel.stage = 0;
                role!.setStar(0);
                role.setLevel(1);
                role.checkBookStar();
                smc.book.BookModel.books.set(role.RoleModel.id, role);

                //穿默认装备
                [5005, 5006, 5007, 5008].forEach(element => {
                    var e1 = ecs.getEntity<Equip>(Equip);
                    e1.EquipModel.id = element;
                    e1.wear(role);
                    e1.setLevel(1);
                });
                //设置默认技能
                this.setRoleSkill(role);
            }
        }
    }
    private setRoleSkill(role: Role) {
        var table = role.RoleModel.table;
        if (table.id == 312)
            console
        if (table.normal) {
            var normalSkill = ecs.getEntity<Skill>(Skill);
            normalSkill.SkillModel.id = table.normal;
            normalSkill.learn(role);
            role.RoleModelSkill.normal = normalSkill;
        }
        if (table.skill) {
            var skillSkill = ecs.getEntity<Skill>(Skill);
            skillSkill.SkillModel.id = table.skill;
            skillSkill.learn(role);
            role.RoleModelSkill.power = skillSkill;
        }
        if (table.passives) {
            (table.passives as Array<number>).forEach(element => {
                let passivesSkill = ecs.getEntity<Skill>(Skill);
                passivesSkill.SkillModel.id = element;
                passivesSkill.learn(role);
                role.RoleModelSkill.passives.push(passivesSkill);
            });
        }
        if (table.skillAwake) {
            var skillSkillAwake = ecs.getEntity<Skill>(Skill);
            skillSkillAwake.SkillModel.id = table.skillAwake;
            skillSkillAwake.learn(role);
            role.RoleModelSkill.powerAwake = skillSkillAwake;
        }

        if (table.passivesAwake) {
            (table.passivesAwake as Array<number>).forEach(element => {
                let passivesSkillAwake = ecs.getEntity<Skill>(Skill);
                passivesSkillAwake.SkillModel.id = element;
                passivesSkillAwake.learn(role);
                role.RoleModelSkill.passivesAwake.push(passivesSkillAwake);
            });
        }
    }

    private loadBag(e: Account) {
        return new Promise((resolve, reject) => {
            var params: any = {}
            let onComplete = {
                target: this,
                callback: (data: any) => {
                    var bag = data.data.bag;
                    e.initItemByServer(bag);
                    resolve(null);
                }
            }
            // 请求登录游戏获取角色数据
            // netChannel.game.req("user", "loadallhero", params, onComplete);
            onComplete.callback({
                data: {
                    bag: {
                        "zhenzhu": 0,
                        "card_104": 0,
                        "libao_mengyanjingshi": 0,
                        "libao_coin": 0,
                        "banshou": 0,
                        "card_227": 0,
                        "box_lv2": 2,
                        "card_102": 0,
                        "zongzi_lv2": 0,
                        "card_308": 0,
                        "xianshenmenpiao": 0,
                        "gold": 182,
                        "jinjieshi": 58,
                        "yugan_lv2": 0,
                        "card_303": 0,
                        "box_lv4": 0,
                        "card_210": 0,
                        "saodangmotan": 0,
                        "card_111": 0,
                        "libao_banshou": 0,
                        "card_305": 0,
                        "card_211": 0,
                        "libao_baiyu": 0,
                        "card_115": 0,
                        "zongzi_lv1": 0,
                        "card_101": 0,
                        "card_313": 0,
                        "card_107": 0,
                        "libao_jinjieshi": 0,
                        "jingtie": 0,
                        "libao_jubaoding_lv2": 0,
                        "card_106": 0,
                        "zhishibi": 2,
                        "nengliang": 100,
                        "coin": 1680,
                        "zongzi_lv3": 0,
                        "card_112": 0,
                        "huoba_lv1": 10,
                        "card_302": 0,
                        "libao_wujiang_hong1": 0,
                        "libao_wujiang_cheng2": 0,
                        "card_110": 0,
                        "card_114": 0,
                        "yugan_lv1": 0,
                        "card_301": 0,
                        "card_212": 0,
                        "juntuanbi": 0,
                        "libao_gold": 0,
                        "card_228": 0,
                        "mengyanjingshi": 0,
                        "card_314": 0,
                        "libao_wujiang_zi1": 0,
                        "box_lv1": 10,
                        "card_209": 0,
                        "huoba_lv2": 0,
                        "zhuweibi": 0,
                        "caiyu": 0,
                        "baiyu": 0,
                        "xiaoyugan": 10,
                        "box_lv5": 0,
                        "card_207": 0,
                        "zhaomuling": 0,
                        "card_223": 0,
                        "card_220": 0,
                        "huoba_lv3": 0,
                        "libao_jingtie": 0,
                        "card_215": 0,
                        "card_203": 0,
                        "libao_jubaoding_lv1": 0,
                        "shendeng": 0,
                        "card_103": 0,
                        "card_105": 0,
                        "card_108": 0,
                        "card_109": 0,
                        "libao_wujiang_hong2": 0,
                        "card_116": 0,
                        "card_304": 0,
                        "card_202": 0,
                        "card_113": 0,
                        "box_lv3": 0,
                        "pifubi": 0,
                        "card_306": 0,
                        "card_307": 0,
                        "card_208": 0,
                        "libao_wujiang_cheng1": 0,
                        "card_312": 0
                    }
                }
            });
        })
    }


}
